<?php

namespace app\admin\service;

use app\model\VideoTypes;

class TypeService
{
    private $model;

    public function __construct(VideoTypes $model)
    {
        $this->model = $model;
    }

    // 添加/编辑分类
    public function editType($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
        } else {
            $params['updated_at'] = time();
        }

        $id = $this->model->edit($params);

        if (!$id) {
            return false;
        }
        return true;
    }

    // 获取分类列表
    public function getTypeDetail($id)
    {
        $data = $this->model->getInfoById($id, 'id, name, sort, is_test');

        return $data;
    }

    // 获取分类列表
    public function getTypeList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 15;

        $where[] = ['is_delete', '=', 0];

        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        $data = $this->model->getPagingDataByWhere($where, $page, $limit, 'id, name, sort, is_test, created_at');

        return $data;
    }

    // 获取分类下拉列表
    public function getTypeSelectlist()
    {
        $where[] = ['is_delete', '=', 0];
        $data = $this->model->getAllData($where);
        return $data;
    }

    // 删除分类
    public function deleteType($params)
    {
        $params['is_delete'] = 1;
        $params['updated_at'] = time();

        return $this->model->edit($params);
    }
}